﻿using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Xml;
using System.Xml.Serialization;
using Common.Logging;
using In.Api.Dto.Notify;
using In.Api.HttpClients;
using Newtonsoft.Json;

namespace In.Api.Controllers
{
    public class NotifyController : ApiController
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof(NotifyController));

        [Route("Notify/CreateQRCode")]
        [HttpPost]
        public HttpResponseMessage CreateQrCode()
        {
            try
            {
                string cardNo = this.Request.Content.ReadAsStringAsync().Result;
                if (string.IsNullOrEmpty(cardNo))
                {
                    return new HttpResponseMessage()
                    {
                        StatusCode = HttpStatusCode.OK,
                        Content = new StringContent(ToXml(new NotificationResponse()
                        {
                            ResultCode = "1",
                            ResultDesc = "错误：卡号不能为空",
                            ResponseInfo = "http://weixin.qq.com/r/EkN1bb3E6OCXrfkO9xYA"
                        }))
                    };
                }

                var httpClient = OutHttpClient.GetClient();
                var qrCodeUrl = httpClient.GetStringAsync("Notify/CreateQRCode?cardNo=" + cardNo).Result;
                if (string.IsNullOrEmpty(qrCodeUrl)
                    || qrCodeUrl.IndexOf("http://weixin.qq.com/", StringComparison.Ordinal) < 0)
                {
                    return new HttpResponseMessage()
                    {
                        StatusCode = HttpStatusCode.OK,
                        Content = new StringContent(ToXml(new NotificationResponse()
                        {
                            ResultCode = "0",
                            ResultDesc = "",
                            ResponseInfo = "http://weixin.qq.com/r/EkN1bb3E6OCXrfkO9xYA"
                        }))
                    };
                }

                return new HttpResponseMessage()
                {
                    StatusCode = HttpStatusCode.OK,
                    Content = new StringContent(ToXml(new NotificationResponse()
                    {
                        ResultCode = "0",
                        ResultDesc = "",
                        ResponseInfo = qrCodeUrl
                    }))
                };
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("调用院外CreateQRCode时出错，信息:{0}", JsonConvert.SerializeObject(ex));

                return new HttpResponseMessage()
                {
                    StatusCode = HttpStatusCode.OK,
                    Content = new StringContent(ToXml(new NotificationResponse()
                    {
                        ResultCode = "0",
                        ResultDesc = "",
                        ResponseInfo = "http://weixin.qq.com/r/EkN1bb3E6OCXrfkO9xYA"
                    }))
                };
            }
        }

        public string ToXml(Object oObject)
        {
            var xmlDoc = new XmlDocument();
            var xmlSerializer = new XmlSerializer(oObject.GetType());
            using (var xmlStream = new MemoryStream())
            {
                xmlSerializer.Serialize(xmlStream, oObject);
                xmlStream.Position = 0;
                xmlDoc.Load(xmlStream);
                return xmlDoc.InnerXml;
            }
        }
    }
}